package com.tigercar.servicemanagement.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tigercar.common.pojo.PageResult;
import com.tigercar.common.utils.BeanHelper;
import com.tigercar.servicemanagement.dto.EmployeeDTO;
import com.tigercar.servicemanagement.dto.UserDTO;
import com.tigercar.servicemanagement.mapper.UserMapper;
import com.tigercar.servicemanagement.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    public PageResult<UserDTO> findUser(Integer page, Integer rows, String key) {

        //分页参数
        IPage<Employee> iPage = new Page<>(page,rows);

        //查询条件
        QueryWrapper<Employee> queryWrapper = Wrappers.query();

        if (StringUtils.isNotEmpty(key)){

            queryWrapper.and(
                    i->i.like("username",key)
                            .or()
                            .like("employeename",key)
                            .or()
                            .like("phone",key)
                            .or()
                            .like("deptname",key)
                            .or()
                            .like("rolename",key)
                            .or()
                            .like("state",key)
            );
        }

        iPage = userMapper.selectPage(iPage, queryWrapper);

        List<Employee> userList = iPage.getRecords();
        List<UserDTO> userDTOList = BeanHelper.copyWithCollection(userList,UserDTO.class);
        getResult(userDTOList);
        PageResult<UserDTO> pageResult = new PageResult<>(iPage.getTotal(),iPage.getPages(),userDTOList);

        return pageResult;
    }

    private void getResult(List<UserDTO> userDTOList) {

        userDTOList.forEach(userDTO -> {
            Employee employee = this.findUserById(userDTO.getId());
            userDTO.setEmployeename(employee.getEmployeename());
            userDTO.setPhone(employee.getPhone());
            userDTO.setEmail(employee.getEmail());
            userDTO.setDeptname(employee.getDeptname());
        });
    }

    public Employee findUserById(Integer id) {
        Employee employee = userMapper.selectById(id);
        return employee;
    }

    public void addUser(List<String> employeename, List<Integer> rolename, String password, String rePassword, Employee employee) {

        try {
            userMapper.insert(employee);

            userMapper.saveUser(employee.getId(),employeename,rolename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
